//
//  Connection.m
//  Keyvaluecoding
//
//  Created by 赵洋 on 14/11/7.
//  Copyright (c) 2014年 赵洋. All rights reserved.
//

#import "Connection.h"
#import "Reachability.h"
#import "Savefile.h"
@implementation Connection
- (void)dealloc
{
    Block_release(_datablock);
    [_urlname release];
    [_recieve release];
    [super dealloc];
}

+ (BOOL)judgeConnectEnabled
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            return YES;
        }
            break;
        case ReachableViaWWAN:
        {
            return YES;
        }
            break;
        default:
            break;
    }
}

+ (void)GetConnectWithUrl:(NSString *)url value:(NSMutableDictionary *)value block:(finishedBlock)block
{
    if ([Connection judgeConnectEnabled]) {
        Connection *model = [[Connection alloc] init];
        model.datablock = block;
        [model startWithUrl:url value:value];
        [model release];
    } else {
        // 网址和参数拼接完的接口
            NSString *name = [Connection urlWithurlStr:url value:value];
            name = [name stringByReplacingOccurrencesOfString:@"/" withString:@""];
            NSData *data = [Savefile readFileWithName:name];
        if (data) {
            block(data);
        }
     }
}

+ (void)PostconnectWithUrl:(NSString *)url body:(NSString *)body block:(finishedBlock)block
{
    Connection *model = [[Connection alloc] init];
    model.datablock = block;
    [model startWithPostUrl:url body:body];
    [model release];
}



- (instancetype)init
{
    self = [super init];
    if (self) {
        self.recieve = [NSMutableData data];
        
    }
    return self;
}
// 获取get请求的网址
+ (NSString *)urlWithurlStr:(NSString *)url value:(NSMutableDictionary *)value
{
    NSString *str = @"";
    for (NSString *key in [value allKeys]) {
        if ([str length] == 0) {
            str = [NSString stringWithFormat:@"?%@=%@", key, [value objectForKey:key]];
        } else {
            str = [NSString stringWithFormat:@"%@&%@=%@", str, key, [value objectForKey:key]];
        }
    }
    NSString *string = [NSString stringWithFormat:@"%@%@", url, str];
    return string;

}


- (void)startWithUrl:(NSString *)url value:(NSMutableDictionary *)value
{
    url = [Connection urlWithurlStr:url value:value];
    self.urlname = [NSString stringWithFormat:@"%@", url];
    
    url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *myurl = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myurl];
    [request setHTTPMethod:@"GET"];
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}

- (void)startWithPostUrl:(NSString *)url body:(NSString *)body
{
    url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *myurl = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myurl];
    [request setHTTPMethod:@"POST"];
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection connectionWithRequest:request delegate:self];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recieve appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
        
        self.datablock(_recieve);
        // 根据网址名 来存储数据, 网址即为文件名
        [self.urlname stringByReplacingOccurrencesOfString:@"/" withString:@""];
        [Savefile writeFileWithName:self.urlname data:_recieve];
    
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error = %@", error);
}



@end
